package CB_C19;

import javax.media.*;
import javax.media.Processor;
import javax.media.protocol.*;
import javax.media.control.*;
import javax.media.Format;
import javax.media.format.*;

public class MyProcessor implements ControllerListener
{
    //***source for processor***//
    private MediaLocator src;
    public DataSource data = null;
    public Processor proc = null;

    //***videoForamt input without convertion***//
    public VideoFormat in;
    //***tracks of media***//
    private TrackControl tracks[];
    //***videoTrack of media***//
    public TrackControl videoTrack;
    //***Properties***//
    private int brightness;
    private int strength;
    private Boolean random;
    private String duration;
    private String begin;
    private boolean effect_set;
    
    public double currenttime = 0;
    
    //***Effects(framedrop + brightness***//
    public Effect effect;

    //***Player for getVisualComponents***//
    Player player;
    DataSource output_processor;

    /**MyProcessor Created on 07.1.2010. <br>
     * <br>
     * Represents our Processor and Player<br>
     * @author Sebastian Czekierski, Stefan Blumberger
     */
    MyProcessor(String str_input,Boolean on,int b,int s,Boolean r,String d,String be,boolean effect_set)
    {
       src = new MediaLocator(str_input);
       this.setproperties(on, b, s, r, d, be);
       this.effect_set = effect_set;
       init();
    }
    void init()
    {
       try
       {
           Manager.setHint(Manager.LIGHTWEIGHT_RENDERER,new Boolean(true));
           data = Manager.createDataSource(src);
           proc = Manager.createProcessor(data);
           proc.addControllerListener(this);
           proc.configure();

       }catch(Exception e)
       {
           e.printStackTrace();
           System.out.print("EPIC FAIL!!!!!!!!!!"); ///TODO Fehlerbehandlung
       }
    }
    public synchronized void controllerUpdate(ControllerEvent event)
    {

        if (event instanceof ConfigureCompleteEvent)
        {
            tracks = proc.getTrackControls();
            if (tracks == null)
            {
                System.out.println("Failed to obtain track controls from the processor.");
            }
            for(int i = 0; i < tracks.length; i++)
            {
                if (tracks[i].getFormat() instanceof VideoFormat)
                {
                    videoTrack = tracks[i];
                    break;
                }
            }
            if (videoTrack == null)
            {
                System.out.println("The input media does not contain a video track.");
            }
            else
            {
                in = (VideoFormat)videoTrack.getFormat();
                videoTrack.setFormat(new RGBFormat(
                                in.getSize(),
                                in.getMaxDataLength(),
                                Format.intArray,
                                in.getFrameRate(),
                                32, Format.NOT_SPECIFIED,
                                Format.NOT_SPECIFIED,
                                Format.NOT_SPECIFIED));
                if(effect_set)
                {
                    effect = new Effect_Brightness(this.brightness);
                    effect.setInputFormat(videoTrack.getFormat());
                }
                else
                {
                    effect = new MyEffect(this.random, this.strength, this.duration, this.begin,in.getFrameRate());
                    effect.setInputFormat(videoTrack.getFormat());
                }
                Codec codec[] ={effect};
                try
                {
                    videoTrack.setCodecChain(codec);
                } catch (UnsupportedPlugInException ex)
                {
                    ex.printStackTrace();
                }
            }
            //-----------------------------------------------------
            proc.setContentDescriptor(new ContentDescriptor(ContentDescriptor.RAW));
            //proc.setContentDescriptor(null);
            proc.realize();
	}
        if (event instanceof RealizeCompleteEvent)
        {
            System.out.println("realized");
            output_processor = proc.getDataOutput();
            try
            {
                player = Manager.createRealizedPlayer(output_processor);
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        if (event instanceof EndOfMediaEvent)
        {
           player.stop();
           proc.stop();
           proc.setMediaTime(new Time(0));
           player.setMediaTime(new Time(0));
        }
    }
    public java.awt.Component getVisualComponent()
    {
        return player.getVisualComponent();
    }
    public void playerstop()
    {
        player.stop();
        proc.stop();
    }
    public void playerstart()
    {
        player.start();
        proc.start();
    }
    public void setproperties(Boolean on,int b,int s,Boolean r,String d,String be)
    {
            if(on)
            {
                brightness = b;
                strength = s;
                random = r;
                duration = d;
                begin = be;
            }
            else
            {
                brightness = 100;
                strength = 0;
                random = false;
                duration = "0";
                begin = "0";
            }
    }
    public void settime(double currenttime)
    {
        this.currenttime = currenttime;
    }
}
